package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.MovieSession;
import com.bigdata.movieticketapi.mapper.MovieOrderMapper;
import com.bigdata.movieticketapi.mapper.MovieSessionMapper;
import com.bigdata.movieticketapi.service.IMovieSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.GetDataByIdReqVo;
import com.bigdata.movieticketapi.vo.req.MovieSessionReqVO;
import com.bigdata.movieticketapi.vo.req.PageMovieSessionReqVO;
import com.bigdata.movieticketapi.vo.resp.MovieSessionDetailRespVO;
import com.bigdata.movieticketapi.vo.resp.PageMovieSessionRespVO;
import com.bigdata.movieticketapi.vo.resp.SoldMovieSeatRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-09
 */
@Service
public class MovieSessionServiceImpl extends ServiceImpl<MovieSessionMapper, MovieSession> implements IMovieSessionService {

    @Resource
    private MovieSessionMapper movieSessionMapper;

    @Resource
    private MovieOrderMapper movieOrderMapper;

    @Override
    public void addMovieSession(MovieSessionReqVO vo) {
        MovieSession insertMovieSession = new MovieSession();
        BeanUtils.copyProperties(vo,insertMovieSession);
        int result = movieSessionMapper.insert(insertMovieSession);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateMovieSession(MovieSession movieSession) {
        int result = movieSessionMapper.updateById(movieSession);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageMovieSessionRespVO> pageMovieSessions(PageMovieSessionReqVO vo) {
        IPage<PageMovieSessionRespVO> result = movieSessionMapper.selectPageMovieSessions(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }

    @Override
    public PageResult<PageMovieSessionRespVO> pageMovieSessionsById(GetDataByIdReqVo vo) {
        IPage<PageMovieSessionRespVO> result = movieSessionMapper.selectPageMovieSessionsById(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }

    @Override
    public MovieSessionDetailRespVO getSessionDetailById(GetDataByIdReqVo vo) {
        MovieSessionDetailRespVO result = movieSessionMapper.selectSessionDetailById(vo);
        result.setSoldMovieSeat(movieOrderMapper.selectSoldMovieSeat(vo));
        return result;
    }
}
